package com.aaa.outbound.service.impl;

import com.aaa.entity.outbound.Outorder;
import com.aaa.outbound.mapper.OutorderMapper;
import com.aaa.outbound.service.OutorderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wjf
 * @since 2020-11-23
 */
@Service
public class OutorderServiceImpl extends ServiceImpl<OutorderMapper, Outorder> implements OutorderService {
    @Autowired
    private OutorderMapper outorderMapper;

    @Override
    public Page<Outorder> searchOutOrder(Integer page, Integer limit, Outorder outorder) {
        Page<Outorder> pageinfo=new Page<>(page,limit);
        QueryWrapper<Outorder> queryWrapper=new QueryWrapper<>();
        String outorderno=outorder.getOutorderno();//出库单号
        String state=outorder.getState();//订单状态
        String receiver=outorder.getReceiver();//收件人
        Date begin=outorder.getBeginTime();//查询出库开始时间
        Date end=outorder.getEndTime();//查询出库截至时间
        String province=outorder.getProvince();//省
        String city=outorder.getCity();//市
        String county=outorder.getCounty();//县
        if (StringUtils.isNotEmpty(outorderno)){
            queryWrapper.like("outorderno",outorderno);
        }
        if (StringUtils.isNotEmpty(state)){
            queryWrapper.like("state",state);
        }
        if (StringUtils.isNotEmpty(receiver)){
            queryWrapper.like("receiver",receiver);
        }
        if (StringUtils.isNotEmpty(province)){
            queryWrapper.like("province",province);
        }
        if (StringUtils.isNotEmpty(city)){
            queryWrapper.like("city",city);
        }
        if (StringUtils.isNotEmpty(county)){
            queryWrapper.like("county",county);
        }
        if (begin!=null&&end==null){
            queryWrapper.gt("depottime",begin);
        }else if(begin==null&&end!=null){
            queryWrapper.lt("depottime",end);
        }else if (begin!=null&&end!=null){
            queryWrapper.between("depottime",begin,end);
        }
        Page<Outorder> outorderPage = outorderMapper.selectPage(pageinfo,queryWrapper);
        return outorderPage;
    }

    @Override
    public Integer addOutOrder(Outorder outorder) {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        outorder.setId(null);
        outorder.setOutorderno("OT"+dateFormat.format(now));
        outorder.setState("0");
        return outorderMapper.insert(outorder);
    }

    @Override
    public List<Map> findState() {
        return outorderMapper.findState();
    }

    @Override
    public Outorder selectIdByOutorderno(String outorderno) {
        return outorderMapper.selectIdByOutorderno(outorderno);
    }

    @Override
    public String selectPicktasknoByOutorderno(String outorderno) {
        return outorderMapper.selectPicktasknoByOutorderno(outorderno);
    }

    @Override
    public List<Map> findAllSheng() {
        return this.baseMapper.findAllSheng();
    }

    @Override
    public List<Map> findAllShiById(String cityname) {
        return this.baseMapper.findAllShiById(cityname);
    }

    @Override
    public List<Map> findAllXianById(String cityname) {
        return this.baseMapper.findAllXianById(cityname);
    }

    /**
     * 省市级联动
     * @return
     */
    @Override
    public List<Map> findCity() {
        List<Map> city = outorderMapper.findCity();
        List<Map> parent=new ArrayList<>();
        for (Map map : city) {
            if (map.get("type").equals(1)){
                parent.add(map);
            }
        }
        for (Map map : parent) {
            sonCreate(map,city);
        }
        return parent;
    }
    public String sonCreate(Map parent,List<Map> city){
        List<Map> list=new ArrayList<>();
        for (Map map : city) {
            if (parent.get("id").equals(map.get("pid"))){
                list.add(map);
            }
        }
        for (Map map : list) {
            sonCreate(map,city);
        }
        parent.put("son",list);
        return "结束";
    }
}
